package com.easepal802s.project.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.clj.fastble.data.BleDevice;
import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;
import com.easepal802s.project.ble.HexUtils;
import com.easepal802s.project.observer.BleDataObserver;
import com.ogawa.base.utils.CommonUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by FHB on 2018/4/8.
 */
public class LayoutBody extends FrameLayout implements BleDataObserver.OnBleListerner {
    private ImageView mIvAirShoulder;//肩部气囊
    private ImageView mIvAirArm;//小手臂气囊
    private ImageView mIvAirSeat;//座位气囊
    private ImageView mIvAirCalf;//小腿气囊
    private ImageView mIvAirAnkle;//腿部气囊（脚踝跟腿部一致）
    private ImageView mIvAirFoot;//腿部气囊

    private ImageView mIvHotBack;//背部加热
    private ImageView mIvHotSeat;//座位加热
    private ImageView mIvHotCalf;//小腿加热

    private ImageView mIvFootRollOne;
    private ImageView mIvFootRollTwo;

    private ImageView mIvKnead;//揉捏
    private ImageView mIvZhiya;//指压
    private ImageView mIvSwedish;//瑞典式
    private ImageView mIvMassage;//推拿
    private ImageView mIvPat;//敲揉
    private ImageView mIvKnock;//敲击
    private ImageView mBodyPoint1;
    private Context mContext;
    private String mData;
    private List<ImageView> mPointViews = new ArrayList<>();
    private byte mByteAnimator1;
    private byte mByteAnimator2;
    private byte mBytePoints1;
    private byte mBytePoints2;
    private byte mByteAir;
    private String mDataFoot;
    private String mDataHot;

    public static final int KNEAD_ON = 1;
    public static final int KNEAD_OFF = 2;
    private List<ImageView> mAnimatorImage = new ArrayList<>();
    private MyHandler myHandler;
    public LayoutBody(Context context) {
        super(context);
    }

    public LayoutBody(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        myHandler = new MyHandler(this);
        BleDataObserver.getInst().registerBleDataObserver(this);
        mBodyPoint1 = findViewById(R.id.body_point1);

        mIvFootRollOne = findViewById(R.id.foot_roll1);
        mIvFootRollTwo = findViewById(R.id.foot_roll2);
        mIvAirShoulder = findViewById(R.id.air_shoulder);
        mIvAirArm = findViewById(R.id.air_arm);
        mIvAirSeat = findViewById(R.id.air_seat);
        mIvAirCalf = findViewById(R.id.air_calf);
        mIvAirAnkle = findViewById(R.id.air_ankle);
        mIvAirFoot = findViewById(R.id.air_foot);

        mIvHotBack = findViewById(R.id.hot_back);
        mIvHotSeat = findViewById(R.id.hot_seat);
        mIvHotCalf = findViewById(R.id.hot_calf);

        mIvKnead = findViewById(R.id.knead);
        mIvZhiya = findViewById(R.id.zhiya);
        mIvSwedish = findViewById(R.id.swedish);
        mIvMassage = findViewById(R.id.massage);
        mIvPat = findViewById(R.id.pat);
        mIvKnock = findViewById(R.id.knock);
        mAnimatorImage.clear();
        mAnimatorImage.add(mIvKnead);
        mAnimatorImage.add(mIvZhiya);
        mAnimatorImage.add(mIvSwedish);
        mAnimatorImage.add(mIvMassage);
        mAnimatorImage.add(mIvPat);
        mAnimatorImage.add(mIvKnock);
        addPointViews();
//        Animation alphaAnimation = new AlphaAnimation( 1, 0.4f );
//        alphaAnimation.setDuration( 300 );
//        alphaAnimation.setInterpolator( new LinearInterpolator( ) );
//        alphaAnimation.setRepeatCount( Animation.INFINITE );
//        alphaAnimation.setRepeatMode( Animation.REVERSE );
//        mIvKnead.startAnimation( alphaAnimation );
//
//
//        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mIvZhiya, "alpha",
//                0.0f, 1.0f,0.0f);
//        objectAnimator1.setDuration(1000);
//        objectAnimator1.setRepeatCount(10000);
//        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mIvZhiya, "alpha",
//                0.0f, 1.0f);
//        objectAnimator1.setDuration(1000);
//        objectAnimator1.setRepeatCount(10000);
//        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mIvSwedish, "alpha",
//                0.0f, 1.0f,0.0f);
//        objectAnimator1.setDuration(1000);
//        objectAnimator1.setRepeatCount(10000);
//        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(mIvMassage, "alpha",
//                1.0f, 0.0f,1.0f);
//        objectAnimator1.setDuration(1000);
//        objectAnimator1.setRepeatCount(10000);
//        ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(mIvPat, "alpha",
//                0.0f, 1.0f);
//        objectAnimator1.setDuration(1000);
//        objectAnimator1.setRepeatCount(10000);
//
//        ObjectAnimator objectAnimator6 = ObjectAnimator.ofFloat(mIvKnock, "alpha",
//                0.0f, 1.0f);
//        objectAnimator1.setDuration(1000);
//        objectAnimator1.setRepeatCount(10000);
//        mAnimatorList.add(objectAnimator1);
//        mAnimatorList.add(objectAnimator2);
//        mAnimatorList.add(objectAnimator3);
//        mAnimatorList.add(objectAnimator4);
//        mAnimatorList.add(objectAnimator5);
//        mAnimatorList.add(objectAnimator6);
//        objectAnimator4.start();
    }


    public void onDestory(){
        try {
            BleDataObserver.getInst().removeBleDataObserver(this);
        }
        catch (Exception e){
        }
       stopAnimation();

    }

    public void showFootRoll(boolean isShow){
        if(isShow){
            mIvFootRollTwo.setVisibility(VISIBLE);
            mIvFootRollOne.setVisibility(VISIBLE);
        }
        else {
            mIvFootRollTwo.setVisibility(GONE);
            mIvFootRollOne.setVisibility(GONE);
        }
    }
    /**
    *腰部加热
*/
    public void setHotBackShow(boolean isShow){
        if(isShow){
            mIvHotBack.setVisibility(VISIBLE);

        }
        else {
            mIvHotBack.setVisibility(GONE);

        }
    }
        /**
    *座侧加热
*/
    public void setHotSeatShow(boolean isShow){
        if(isShow){

            mIvHotSeat.setVisibility(VISIBLE);
        }
        else {

            mIvHotSeat.setVisibility(GONE);
        }
    }
        /**
    *小腿加热
*/
    public void setHotCalfShow(boolean isShow){
        if(isShow){
            mIvHotCalf.setVisibility(VISIBLE);
        }
        else {
            mIvHotCalf.setVisibility(GONE);
        }
    }

    /**
     * 肩部气囊
     * @param isShow
     */
    public void setAirShoulderShow(boolean isShow){
        if(isShow){
            mIvAirShoulder.setVisibility(VISIBLE);
        }
        else {
            mIvAirShoulder.setVisibility(GONE);
        }
    }
    /**
     * 手部气囊
     * @param isShow
     */
    public void setAirHandShow(boolean isShow){
        if(isShow){
            mIvAirArm.setVisibility(VISIBLE);
        }
        else {
            mIvAirArm.setVisibility(GONE);
        }
    }
    /**
     * 腰部气囊
     * @param isShow
     */
    public void setAirSeatShow(boolean isShow){
        if(isShow){
            mIvAirSeat.setVisibility(VISIBLE);
        }
        else {
            mIvAirSeat.setVisibility(GONE);
        }
    }
    /**
     * 小腿气囊
     * @param isShow
     */
    public void setAirCalfShow(boolean isShow){
        if(isShow){
            mIvAirCalf.setVisibility(VISIBLE);
        }
        else {
            mIvAirCalf.setVisibility(GONE);
        }
    }

    /**
     * 脚部气囊
     * @param isShow
     */
    public void setAirFootShow(boolean isShow){
        if(isShow){
            mIvAirAnkle.setVisibility(VISIBLE);
            mIvAirFoot.setVisibility(VISIBLE);
        }
        else {
            mIvAirAnkle.setVisibility(GONE);
            mIvAirFoot.setVisibility(GONE);
        }
    }



    // 开始动画 小图标从上往下0 -- 5；
    public void setStartPosition(int position) {
        for(int i = 0;i<mAnimatorImage.size();i++){
            if(position == i){
                CommonUtil.startFlick(mAnimatorImage.get(i));
            }

        }

    }
      // 开始动画 小图标从上往下0 -- 5；
    public void setStopPosition(int position) {
        for(int i = 0;i<mAnimatorImage.size();i++){
            if(position == i){
                CommonUtil.stopFlick(mAnimatorImage.get(i));
            }

        }

    }
    private void stopAnimation(){
        //停止所有动画
        for(int i = 0;i<mAnimatorImage.size();i++){
            CommonUtil.stopFlick(mAnimatorImage.get(i));
        }
    }





    @Override
    public void onConnectState(int state) {

    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }

    @Override
    public void onGetData(String data) {
        myHandler.obtainMessage(100,data).sendToTarget();
        myHandler.obtainMessage(101,data).sendToTarget();
        myHandler.obtainMessage(102,data).sendToTarget();
        myHandler.obtainMessage(103,data).sendToTarget();
        myHandler.obtainMessage(104,data).sendToTarget();
    }

    private  class MyHandler extends Handler {
        WeakReference<LayoutBody> mLayoutBodyWeakReference;

        MyHandler(LayoutBody mLayoutBody) {
            mLayoutBodyWeakReference = new WeakReference<>(mLayoutBody);
        }

        @Override
        public void handleMessage(Message msg) {
            final LayoutBody mLayoutBody = mLayoutBodyWeakReference.get();
            if (mLayoutBody != null) {
                String data  = (String) msg.obj;
                switch (msg.what){
                    case 100://背部机芯
                        getPoints(data);
                        break;
                    case 101:// 运行模式动画;
                        showAnimation(data);
                        break;
                    case 102:// 脚底滚轮;
                        showFoot(data);
                        break;
                    case 103:// 加热;
                        showHot(data);
                        break;
                    case 104:// 气囊;
                        getAirBag(data);
                        break;
                }



            }
        }
    }


    /**
     * 显示正在运行程序的动画
     * @param data
     */
    private void showAnimation(String data){
        if (data.startsWith(BleConstant.BAG1_HEAD)) {

            byte[] buffer = HexUtils.hexToByte(data);
            Log.e("mData7_8",Arrays.toString(buffer));
            byte  byte7 = buffer[7];
            byte byte8 = buffer[8];
            //当数据有变化时再去判断哪个模式在运行
            if(mByteAnimator1!=byte7||mByteAnimator2!=byte8){
                mByteAnimator1 = byte7;
                mByteAnimator2 =  byte8;
                //byte8  bytePosition就 = 7
                if (CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,0))) {
                    //推拿
                    setStartPosition(3);
                }
                else{
                    setStopPosition(3);
                }
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,1))
                        ||CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,2))){
                    //揉捏
                    setStartPosition(0);
                }
                else{
                    setStopPosition(0);
                }

                 if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,3))
                        ||CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,4))){
                    //瑞典
                    setStartPosition(2);
                }
                else{
                    setStopPosition(2);
                }

                 if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,5))
                        ||CommonUtil.isOpera(HexUtils.getByteInPosition(data,8,0))){
                    //敲击
                    setStartPosition(5);
                }
                   else{
                    setStopPosition(5);
                }

                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,8,1))
                        ||CommonUtil.isOpera(HexUtils.getByteInPosition(data,8,2))){
                    //指压
                    setStartPosition(1);
                }
                   else{
                    setStopPosition(1);
                }

                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,8,3))
                        ||CommonUtil.isOpera(HexUtils.getByteInPosition(data,8,4))){
                    //拍打
                    setStartPosition(4);
                }
                   else{
                    setStopPosition(4);
                }

            }
        }
    }
//显示滚轮
    private void getPoints(String data){
        if (data.startsWith(BleConstant.BAG1_HEAD)) {
            byte[] buffer = HexUtils.hexToByte(data);
            byte byte12 =buffer[12];
            byte byte13 =buffer[13];
            //当数据有变化时再去判断哪个模式在运行
            if(mBytePoints1 !=byte12|| mBytePoints2 !=byte13){
                mBytePoints1 = byte12;
                mBytePoints2 = byte13;
                //byte8  bytePosition就 = 7
                if (CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,0))) {
                    showPoints(10);

                } else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,1))){
                    showPoints(9);
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,2))){
                    showPoints(8);
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,3))){
                    showPoints(7);
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,4))){
                    showPoints(6);
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,5))){
                    showPoints(5);
                } else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,13,0))){
                    showPoints(4);
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,13,1))){
                    showPoints(3);
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,13,2))){
                    showPoints(2);
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,13,3))){
                    showPoints(1);
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,13,4))){
                    showPoints(0);
                }else {
                    showPoints(-1);
                }
            }
        }

    }
    /**
     * 显示脚底滚轮
     * @param data
     */
    private void showFoot(String data){
        if (data.startsWith(BleConstant.BAG2_HEAD)) {
            byte[] buffer = HexUtils.hexToByte(data);
            String footData =HexUtils.getByteInPosition(data,3,1);
            Log.e("showFoot", Arrays.toString(buffer));
            //当数据有变化时再去判断哪个模式在运行
            if(mDataFoot==null||!mDataFoot.equals(footData)){
                mDataFoot = footData;
                if (CommonUtil.isOpera(mDataFoot)) {
                      showFootRoll(true);

                } else {
                    showFootRoll(false);
                }

            }
        }
    }


    /**
     * 显示加热
     * @param data
     */
    private void showHot(String data){
        if (data.startsWith(BleConstant.BAG2_HEAD)) {
            String hotData1 =HexUtils.getByteInPosition(data,3,0);
            String hotData2 =HexUtils.getByteInPosition(data,3,4);
            String hotData3 =HexUtils.getByteInPosition(data,3,5);
            String hot = hotData1+hotData2+hotData3;
            //当数据有变化时再去判断哪个模式在运行
            if(mDataHot==null||!mDataHot.equals(hot)){
                mDataHot = hot;
                setHotBackShow(CommonUtil.isOpera(hotData1));
                setHotSeatShow(CommonUtil.isOpera(hotData2));
                setHotCalfShow(CommonUtil.isOpera(hotData3));

            }
        }
    }

    /**
     * 显示气囊
     * @param data
     */

    private void getAirBag(String data){
        if (data.startsWith(BleConstant.BAG1_HEAD)) {
            byte[] buffer = HexUtils.hexToByte(data);
            byte byte16 = buffer[16];
            Log.e("getAirBag", Arrays.toString(buffer));
            //当数据有变化时再去判断哪个模式在运行
            if(mByteAir!=byte16){
                mByteAir = byte16;
                //byte8  bytePosition就 = 7
                if (CommonUtil.isOpera(HexUtils.getByteInPosition(data,16,0))) {
                    setAirFootShow(true);

                }
                else {
                    setAirFootShow(false);
                }
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,16,1))){
                    setAirCalfShow(true);
                }
                else {
                    setAirCalfShow(false);
                }
               if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,16,2))){
                    setAirHandShow(true);
                }
                else {
                   setAirHandShow(false);
               }
                 if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,16,3))){
                     setAirSeatShow(true);
                }
                else {
                     setAirSeatShow(false);
                }
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,16,5))){
                    setAirShoulderShow(true);
                }
                else {
                 setAirShoulderShow(false);
                }

            }
        }

    }



    @Override
    public void onPowerOff() {
        onDestory();

    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air, String name, String commond) {

    }

    @Override
    public void onShoulderAdjust(int data) {

    }

    private void addPointViews(){
        ImageView mPoint1 = findViewById(R.id.body_point1);
        ImageView mPoint2 = findViewById(R.id.body_point2);
        ImageView mPoint3 = findViewById(R.id.body_point3);
        ImageView mPoint4 = findViewById(R.id.body_point4);
        ImageView mPoint5 = findViewById(R.id.body_point5);
        ImageView mPoint6 = findViewById(R.id.body_point6);
        ImageView mPoint7 = findViewById(R.id.body_point7);
        ImageView mPoint8 = findViewById(R.id.body_point8);
        ImageView mPoint9 = findViewById(R.id.body_point9);
        ImageView mPoint10 = findViewById(R.id.body_point10);
        ImageView mPoint11 = findViewById(R.id.body_point11);
        mPointViews.add(mPoint1);
        mPointViews.add(mPoint2);
        mPointViews.add(mPoint3);
        mPointViews.add(mPoint4);
        mPointViews.add(mPoint5);
        mPointViews.add(mPoint6);
        mPointViews.add(mPoint7);
        mPointViews.add(mPoint8);
        mPointViews.add(mPoint9);
        mPointViews.add(mPoint10);
        mPointViews.add(mPoint11);
    }
    private void showPoints(int position){
        for(int i = 0;i<mPointViews.size();i++){
            if(position== i){
                mPointViews.get(position).setVisibility(VISIBLE);
            }
            else {
                mPointViews.get(i).setVisibility(GONE);
            }
        }
    }
}
